package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000116698728";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCOyVcj3XbqVdQ/GL+p2Gthwct2XkthKj3voiZnPHoLh5mosgQzWGtWQiLMsRibd92EC35azCUYWqDzKaP2HGbUoC4SEI9vObc3rn25ev7OJMoSifHF2hqerKwbDzKGDvgoY6YAQDEPV/LuXB17uTGSopp1PS1lGtNO2bgPWiYPoh2MatNT+HpS8NVw+YTo+2jddM+RiGLk/WtAhKs3IVe4ZRg1pw/8hOjnI38BkdC7gL9Q5whZbMMXIPlnczcnLLM/6ZdqGzz5iXGxd+fbf9I4EjiKQ7b7/dv3ysaSp90LHbkp+w+2eY9HEAuDtUiP/2QOqUjs2IPrE4ZKNCW/ukqZAgMBAAECggEAaF8SPFwpDVfZwplzmFJjA90lvYtlrEu1ZFHxomrZh04CawZc/a83HqFPULJvG0199YR1QyeQQgIVPkmE2WSfYfMIWhoSzqmwnJoGuqsGUGch8nU07nvkE3KCh3EPbGDK0rMsZ2iEtAOExuk8f/C6JMaJAwPKB+qY5T1ORqKNycSN6Vf7nypvlE3PXdaXNit6sKOwPYxg9UT7WoT0oKmrPpLODWrCzfAhck1ulITWS9TJOcWAUxRMJZ4dllvlAsbx6gS+GZTvoQCkZTov38KtLooErMi51nn9Kdez+v97u7gomKmMG0/UURWte1Yd9h0U8jpbSks0a4BmB9iBlW22NQKBgQDIK4pV0f+vgzMi+gXhbfFvuDE5wNjRDm4Wi0bDcnqndMgxkAhNxy9dTFrKmtooHguYlZDXu+vM67DOAliUjalKbueOzL0Xe86xMYDlX3reRL0MFAFZW6lVlSKh0qpVjYP/7bfQ6iyBjjajeIJi8o9wHSJZCh0Q5M1YE9U1Ws7eVwKBgQC2nIVW4ZGY7BEmPgoPtkyaEkknemvOYvHZDtNR/bj1uRO9pxMe/wpWo4m41OP8xHOiIV/vyl+M3k7gODkrFFv+kasdRGbzu/wOyJMnIh2lvyCCIDm6BXsKWUomhTiMgIupfOQiuM6HIB7OBCLlSwoSKV/rHC6UfVkd0kFTffWojwKBgEh3atdy9SvnRaYN+f8IvHWDq16LMHNO1BAZT/8F/N2O68L1r+vohn8yW1QqB3yqk+wjiZJwmAdR9GpAcy0ks/scd3w9Jt/PlJ6Vm2Ju1J6PKBmtTGe7RWyUVztadwwN9ZG7z+Y0QPSONb2srReBVOhzw7GUdb1d9/FBDNkKDQZ1AoGAZf+bQuIPrzj8gz3X5NrQgqaAEfsBSHBf4fuz8XIk46uS6TXvKjsOjUIfZQwUpFUeuYqmyYtiP47Uw8zjsbpXgCPhTiGs16tCqQdDmnIl2TSeddCGLe9twGQLVOTIp0INwIev7e0H/tXkHvKUgzqcz3QozWwaYHp7UnrkXjxXAOUCgYEAhLnoHgKNAFw6Xy2XosHKmGhxBegaA9zYWqDKA5PsKIwuWbK28ZDYNIoyC6dZF17/yTgX0MdahCEbFwCReOXlhOEM+FqeQ2hPjE+ygVx3UNDwxHxyApe95L/rWs8QbPxaoj0liulTFCp1AJSd6d99rkPX9VA5CSVe5yfO9cfYE/U=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAllUq3aku1CtcrIDM0IjjDDx11OLJLwCmTd685X0c0lH3Uy1/fzIBZjMl6nQwpxFV4o/8qxsrSxHWxD9cx7yiOT2dTrhz8G515CCZcYu12ZAzpLZazH0pdrXwuUZ040BDYfANk7JDLfpARHHg1uGFJJUy0yxtTOKXWR0UJSznk7EpGiOIXVinGMGf56Vzozs4DjolUaYjMUayFnxGdFXUGqUbcmVMjmcAff4jER86JTcNT2xV5H9po0nkNOMRGbjzC8Rysmsf9w2SnDnP6w8UFTwE+9wCHWKSelb3ZBwfpKZgpWfuOnma6N171XfjeFF6pDUruo7/QowCd5EPlTWbmQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息 http://otobdct5pb.54http.tech  http://497n86m7k7.52http.net
    private  String notify_url="http://otobdct5pb.54http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();

        //付款金额，必填
        String total_amount = vo.getTotal_amount();

        //订单名称，必填
        String subject = vo.getSubject();

        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;
    }
}
